﻿Public Class AcessoBD
    Implements IAcessoBD

#Region "Enumerações"

    Public Enum eSGBD As Integer
        SQL_SERVER = 0
        MY_SQL = 1
    End Enum

#End Region

#Region "Propriedades"

    Public Shared Property codSGBD As eSGBD
    Public Shared Property sCaminhoAplicacao As String
    Public Shared Property sStringConexao As String

#End Region

#Region "Variáveis"

    Private IAcessoBD As IAcessoBD

#End Region

#Region "Construtores"

    Public Sub New()
        Me.instanciaSGBD(codSGBD)
    End Sub

#End Region


#Region "Métodos"

    Public Sub StringConexao(ByVal sStringConex As String) Implements IAcessoBD.setarStringConexao

    End Sub

    Private Sub instanciaSGBD(ByVal iCodSGBD As Integer)

        Try

            Select Case iCodSGBD

                Case eSGBD.SQL_SERVER
                    Me.IAcessoBD = New conexaoSQLServer

                Case eSGBD.MY_SQL
                    Me.IAcessoBD = New conexaoMySQL

                Case Else
                    Throw New Exception("SGBD não suportado.")

            End Select

        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try

    End Sub

    Public Sub abrirConexao() Implements IAcessoBD.abrirConexao

        Try
            Me.IAcessoBD.setarStringConexao(sStringConexao)
            Me.IAcessoBD.abrirConexao()
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try

    End Sub

    Public Sub fecharConexao() Implements IAcessoBD.fecharConexao
        Try
            Me.IAcessoBD.fecharConexao()
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
    End Sub

    Public Function executarAcao(ByVal sSQL As String, ByVal bAbrirFecharConexao As Boolean) As Integer Implements IAcessoBD.executarAcao

        Try
            Me.IAcessoBD.setarStringConexao(sStringConexao)
            Me.IAcessoBD.executarAcao(sSQL, bAbrirFecharConexao)
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try

    End Function

    Public Function executarConsulta(ByVal sSQL As String, ByVal bAbrirFecharConexao As Boolean) As System.Data.DataTable Implements IAcessoBD.executarConsulta

        Try
            Me.IAcessoBD.setarStringConexao(sStringConexao)

            Return Me.IAcessoBD.executarConsulta(sSQL, bAbrirFecharConexao)
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try

    End Function

    Public Function executarConsultaCampo(ByVal sSQL As String, ByVal bAbrirFecharConexao As Boolean) As Object Implements IAcessoBD.executarConsultaCampo

        Try
            Me.IAcessoBD.setarStringConexao(sStringConexao)
            Return Me.IAcessoBD.executarConsultaCampo(sSQL, bAbrirFecharConexao)
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try

    End Function

    Public Function montaStringConexao(ByVal sSenha As String, ByVal sBanco As String, ByVal sServidor As String, ByVal sUsuario As String) As String Implements IAcessoBD.montaStringConexao

        Try
            Return Me.IAcessoBD.montaStringConexao(sSenha, sBanco, sServidor, sUsuario)
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try

    End Function

    Public Function testarConexao(ByVal sStringConexao As String) As Boolean Implements IAcessoBD.testarConexao
        Dim bRetorno As Boolean = False
        Try
            If Me.IAcessoBD.testarConexao(sStringConexao) Then
                Me.IAcessoBD.setarStringConexao(sStringConexao)

                bRetorno = True
            End If

            Return bRetorno

        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try

    End Function

    Public Sub limparParametros() Implements IAcessoBD.limparParametros
        Try
            Me.IAcessoBD.limparParametros()
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try

    End Sub

    Sub inserirParametro(ByVal sParametro As String, ByVal oValor As Object) Implements IAcessoBD.inserirParametro

        Try
            Me.IAcessoBD.inserirParametro(sParametro, oValor)

        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try

    End Sub

#End Region

End Class
